package com.chuxing.task;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author LiuQi
 * @create 2024-12-19-12:36
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RedisTask {
    private final LettuceConnectionFactory lettuceConnectionFactory;
    /**
     * 每隔30s进行一次连接校验避免连接失效
     */
    @Scheduled(cron="0/30 * * * * ?")
    public void task() {
        try {
            log.debug("正在刷新redis连接可靠性");
            lettuceConnectionFactory.validateConnection();
        } catch (Exception e) {
            log.error("redis连接校验失败");
        }
    }
}
